(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pageBtns();
			teacherSeachMenu();
		});
	});
	
	function pageBtns(){
		check("teacher-cellphone",checkMobilePhone,"checkAddNull");
		check("teacher-name",isNull,"null");
		check("teacher-cardid",checkCard,"check");
		check("teacher-teachercode",checkNum,"checkAddNull");
		check("teacher-birdate",isNull,"null");
		check("teacher-inschooldate",isNull,"null");
		checkTwoDate("teacher-inschooldate","teacher-workdate");
		
		$(".save-btns").click(function(){
			if(checkSave()){
				if(check2()){
					addTeacher();
				}
			}
		});
		
		$("#teacher-cellphone").bind({
			blur:function(){
				if($("#teacher-cellphone").val().length != 0 ){
					var usercode = $("#teacher-cellphone").val();
					if(checkMobilePhone(usercode)){
						$("#teacher-usercode").val(usercode);
						
					}
				}
			},
			focus:function(){
				if($("#teacher-cellphone").val().length != 0 ){
					var usercode = $("#teacher-cellphone").val();
					if(checkMobilePhone(usercode)){
						$("#teacher-usercode").val("");
					}
				}
			}
		});
	}
	
	function addTeacher(){
		var tteacher = getFormData();
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var obj = {};
		for(var key in tteacher){
			if(tteacher[key] != "" && tteacher[key] != undefined){
				obj[key] = tteacher[key];
			}
		}
		obj.schoolcode = schoolcode;
		var data = {
				cmd:"addTeacher",
				model:"teacher",
				userKey:userKey,
				data:obj,
				remark:"",						
		};
		processBegin("正在保存中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("添加教师信息成功");
			}else{
				processError("添加失败,手机号或编码重复");
			}
		},null,null);
	}
	
	function getFormData(){
		new setTTeacher(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
		var tteacher = getTTeacher();
		var obj = {};
		for(var key in tteacher){
			if($("#teacher-"+key)[0]){
				if($("#teacher-"+key)[0].tagName.toLocaleLowerCase() == "input"){
					if(key=="birdate"){
						obj[key] = strToDate($("#teacher-"+key).val());
					}else if(key=="inschooldate"){
						obj[key] = strToDate($("#teacher-"+key).val());
					}else if(key=="workdate"){
						obj[key] = strToDate($("#teacher-"+key).val());
					}else{
						obj[key] = encodeURI($("#teacher-"+key).val());
					}
				}else{
					if(key=="department2"){
						obj[key] = encodeURI($("#teacher-"+key+" option:selected").text());
						obj["departmentcode"] = $("#teacher-"+key+" option:selected").val();
					}else{
						obj[key] = encodeURI($("#teacher-"+key+" option:selected").val());
					}
				}
			}
		}
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		obj.schoolcode = schoolcode;
		new setTTeacher(obj.schoolcode,obj.name,obj.birdate,obj.sex,obj.cellphone,obj.cardid,obj.birpacle,obj.nation,obj.political,obj.phone,obj.remark,obj.teachercode,obj.work,obj.department1,obj.department2,obj.departmentcode,obj.inschooldate,obj.workdate,obj.degree,obj.graduation,obj.tjob,obj.issueorg);
		return getTTeacher();	
	}
	
	function check2(){
		var flg = false;
		var checkFails = $(".check-fail");
		for(var i = 0; i < checkFails.length; i++){
			if(checkFails[i].innerHTML != ""){
				flg = false;
				break;
			}else{
				flg = true;
			}
		}
		return flg;
	}
	
	function checkSave(){
		if($("#teacher-name").val() == ""){
			$("#teacher-name-ex").text("不能为空！");
			return false;
		}else if($("#teacher-birdate").val() == ""){
			$("#teacher-birdate-ex").text("不能为空！");
			return false;
		}else if($("#teacher-cellphone").val() == ""){
			$("#teacher-cellphone-ex").text("不能为空！");
			return false;
		}else if($("#teacher-teachercode").val() == ""){
			$("#teacher-teachercode-ex").text("不能为空！");
			return false;
		}else if($("#teacher-inschooldate").val() == ""){
			$("#teacher-inschooldate-ex").text("不能为空！");
			return false;
		}else if($("#teacher-workdate").val() == ""){
			$("#teacher-workdate-ex").text("不能为空！");
			return false;
		}else{
			return true;
		}
	}
	
	//部门科室二级联动菜单
	function teacherSeachMenu(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getAllDepartments",
				model:"department",
				userKey:userKey,
				data:{
					schoolcode:schoolcode
				},
				remark:"",						
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				departments = data;
				var contents = "<option departmentcodes = '' value='科室'>部门</option>";
				for(var i = 0; i < data.length; i++){
					contents += "<option departmentcodes='"+data[i].departmentcode+"' value='"+data[i].office+"'>"+data[i].department+"</option>";
				}
				$("#teacher-department1").empty();
				$("#teacher-department1").append(contents);
				$("#teacher-department1").unbind("change");
				$("#teacher-department1").change(function(){
					var classes = $("#teacher-department1 option:selected").val();
					var classcodes = $("#teacher-department1 option:selected").attr("departmentcodes");
					$("#teacher-department2").empty();
					var classArr = classes.split(",");
					var classcodeArr = classcodes.split(",");
					var cont = "";
					for(var i = 0; i < classArr.length; i++){
						cont += "<option value='"+classcodeArr[i]+"'>"+classArr[i]+"</option>";
					}
					$("#teacher-department2").append(cont);						
				});
				
			}else{
				
			}
		},null,null);
	} 
	
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}

})();